package cn.mandifal.manager.controller;

import cn.mandifal.advertising.service.AdvertisingService;
import cn.mandifal.pojo.DpContent;
import com.alibaba.dubbo.config.annotation.Reference;
import entity.PageResult;
import entity.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 广告管理控制层
 * @author shun
 */
@RestController
@RequestMapping("/advertisingController")
public class AdvertisingController {
    @Reference(timeout = 3000)
    private AdvertisingService advertisingService;

    /**
     * 返回全部列表
     * @return
     */
    @RequestMapping("/findAll")
    public List<DpContent> findAll(){
        return advertisingService.findAll();
    }


    /**
     * 查询+分页
     * @param dpContent
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/search")
    public PageResult search(@RequestBody DpContent dpContent, int pageNum, int pageSize){
        return advertisingService.search(dpContent, pageNum, pageSize);
    }

    /**
     * 增加
     * @param dpContent
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody DpContent dpContent){
        try {
            //设置默认为 0
            dpContent.setStatus("0");
            advertisingService.add(dpContent);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed();
        }
    }

    /**
     * 修改
     * @param dpContent
     * @return
     */
    @RequestMapping("/update")
    public Result update(@RequestBody DpContent dpContent){
        try {
            advertisingService.update(dpContent);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed();
        }
    }

    /**
     * 获取实体
     * @param id
     * @return
     */
    @RequestMapping("/findOne")
    public DpContent findOne(Long id){
        return advertisingService.findOne(id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Long [] ids){
        try {
            advertisingService.delete(ids);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed();
        }
    }
}
